package com.jvtd.eyl.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.jvtd.base.JvtdMvpFragment;
import com.jvtd.eventBus.EventCenter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.app.MyApplication;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.constants.EventCode;
import com.jvtd.eyl.di.component.DaggerFragmentComponent;
import com.jvtd.eyl.di.component.FragmentComponent;
import com.jvtd.eyl.di.module.FragmentModule;
import com.jvtd.eyl.rxjava.JvtdRxSchedulers;
import com.jvtd.eyl.ui.login.LoginActivity;
import com.jvtd.widget.dialog.JvtdAlertDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseMvpFragment extends JvtdMvpFragment implements MvpView{
    private Unbinder mUnbinder;

    protected FragmentComponent getFragmentComponent()
    {
        return DaggerFragmentComponent.builder()
                .applicationComponent(((MyApplication) mContext.getApplicationContext()).getApplicationComponent())
                .fragmentModule(new FragmentModule(this))
                .build();
    }

    /**
     * fragment degger2 如果未使用 @Inject注解  实现后里面可以不写任何方法
     *
     * @author Chenlei
     * created at 2018/9/25
     **/
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        initInject();
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void bindView(@NonNull View view) {
        mUnbinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onDestroyView() {
        mUnbinder.unbind();
        super.onDestroyView();
    }

    protected abstract void initInject();

    @Override
    public void isLogin(boolean isLogin)
    {
        if (isLogin)
            loginGranted();
        else
            showLoginDialog();
    }

    public void showLoginDialog(){
        new JvtdAlertDialog().setTitle(getString(R.string.warm_prompt_title))
                .setMessage(getString(R.string.you_are_not_login_please_login_title))
                .setOnClickListener((position, inputText) -> {
                    switch (position) {
                        case JvtdAlertDialog.RIGHT://需要跳转登录界面
                            startActivity(LoginActivity.getIntent(mContext));
                            break;
                    }
                })
                .show(getFragmentManager());
    }

    /**
     * 已登录回调
     */
    protected void loginGranted()
    {

    }

    @Override
    public void mustLogout() {
        if (getActivity() != null)
            ((BaseMvpActivity)getActivity()).mustLogout();
    }

    @Override
    public int mustLogoutCode() {
        return JvtdRxSchedulers.CODE_NEED_LOGIN;
    }

    /**
     * 退出登录成功
     */
    @Override
    public void logoutSuccess()
    {
        // 设置页面退出, finish掉当前页面
        if (getActivity() != null)
            getActivity().finish();
    }

    @Override
    public void addBookSuccess(List<BookBean> bookBeans) {
        EventBus.getDefault().post(new EventCenter<>(EventCode.ADD_BOOK_SUCCESS));
    }

    @Override
    public void addBrowseSuccess(BookBean bookBean) {
        EventBus.getDefault().post(new EventCenter<>(EventCode.ADD_BROWSE_SUCCESS));
    }
}
